package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 1 员工列表查询
     * 分页查询
     */
    @GetMapping
//    public Result page(
//            @RequestParam(value = "name", required = false) String name,
//            @RequestParam(value = "gender", required = false) Integer gender,
//            @RequestParam(value = "begin", required = false)
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//            @RequestParam(value = "end", required = false)
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
//            @RequestParam(value = "page", defaultValue = "1") Integer page,
//            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
//    ) {
//        log.info("分页查询,参数:{},{},{},{},{},{}", name, gender, begin, end, page, pageSize);
//        PageResult<Emp> pageResult = empService.page(name, gender, begin, end, page, pageSize);
//        return Result.success(pageResult);
//
//    }
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询,参数:{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);

    }
    
    /**
     * 2 新增员工
     */
    @PostMapping
    public Result add(@RequestBody Emp emp) throws Exception {
        log.info("新增员工数据:{}", emp);
        empService.save(emp);
        return Result.success();
    }
    /**
     * 3 查询全部员工
     */
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询全部员工");
       List<Emp> empList = empService.findAll();
       return Result.success(empList);

    }
    /**
    * 4 删除员工
    */
//    @DeleteMapping
//    public  Result delete(@RequestParam("ids") Integer[] ids){
//        log.info("删除员工,参数:{}", Arrays.toString(ids));
//        return Result.success();
//
//    }
    @DeleteMapping
    public  Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工,参数:{}", Arrays.asList(ids));
        empService.delete(ids);
        return Result.success();

    }
    /**
     *
     * 5 查询回显（为修改员工做准备)
     *
     */
    @GetMapping("/{id}")
    public  Result getInFo(@PathVariable Integer id){
        log.info("查询员工,参数:{}", id);
        Emp emp = empService.getInFo(id);
        return Result.success(emp);

    }
    /**
     * 6 修改员工
     */
    @PutMapping
    public  Result update(@RequestBody Emp emp){
        log.info("修改员工数据:{}",emp);
        empService.update(emp);
        return Result.success();
    }


}
